package mini.statement;

import java.util.List;
import java.util.LinkedList;

import mini.Env;
import mini.expr.Element;
import mini.expr.Expr;


public class ReturnStatement extends Statement {

    private Expr expr;

    public ReturnStatement(Expr e) {
        expr = e;
    }

    public void eval(List<Env> listOfEnvs, LinkedList var) throws ReturnException {
    	// Evaluate
    	Element elem = expr.eval(listOfEnvs, var);
    	// Pop off the last environment
    	if( listOfEnvs != null && listOfEnvs.size() > 0 ) {
    		listOfEnvs.remove( listOfEnvs.size()-1 );
    	}
    	// And return
        throw new ReturnException(elem);
    }
}